mod component;
pub mod event;
mod resource;
mod systems;
mod ui;

use bevy::prelude::*;
use bincode::{Decode, Encode};
use component::{BlockMapPos, CropComponent, CropConfigComponent, FurnitureComponent};
use resource::*;
use systems::*;
use ui::*;

use crate::{config::goods_config::GoodsItem, persistence::Saved};

use super::GameState;

///存档
#[derive(Resource, Encode, Decode, PartialEq, Debug, Default)]
pub struct SavedGame {
    /// 是否首次加载
    pub is_first: bool,
    pub bag: Vec<(usize, Option<(GoodsItem, usize)>)>,
    pub fast: Vec<(usize, Option<(GoodsItem, usize)>)>,
    pub planting_day: PlantingDay,
    pub crop: Vec<(CropComponent, BlockMapPos, CropConfigComponent)>,
    pub field: Vec<BlockMapPos>,
    pub water: Vec<BlockMapPos>,
    pub furniture: Vec<(FurnitureComponent, BlockMapPos)>,
}

impl Saved for SavedGame {
    fn get_def() -> Self {
        Self {
            is_first: true,
            planting_day: PlantingDay::default(),
            ..Default::default()
        }
    }
}

///游戏状态
#[derive(Clone, Copy, Default, Eq, PartialEq, Debug, Hash, States)]
pub enum PlantingUiState {
    #[default]
    ///快捷栏
    FastList,
    ///背包
    Backpack,
}

///种植插件
pub struct PlantingPlugin;

impl Plugin for PlantingPlugin {
    fn build(&self, app: &mut App) {
        app.init_state::<PlantingUiState>()
            .insert_resource(SavedGame::default())
            .insert_resource(PlantingDay::new())
            .insert_resource(MasterBackpack::new())
            .insert_resource(BlockMap::new())
            .insert_resource(FastList::new())
            .add_systems(
                OnTransition {
                    exited: GameState::MainMenu,
                    entered: GameState::LevelLoading,
                },
                (
                    load_game_data,
                    (
                        (init_game_data, spawn_bottom_ui).chain(),
                        spawn_grass,
                        init_ui,
                        create_map_select_block,
                    )
                        .after(load_game_data),
                ),
            )
            .add_systems(
                Update,
                (
                    updata_bottom_last_ui_color,
                    get_mouse_block_map_pos.pipe(handle_click),
                    get_mouse_block_map_pos.pipe(select_map_block),
                    test_btn_click,
                    updata_last_info,
                    updata_bag_info,
                    updata_planting_day_info,
                    updata_grass,
                    updata_planting_day,
                    input_ui_state_change,
                    set_fast_list,
                    set_backpack,
                    packback_move_fast_list,
                    fast_list_move_backpack,
                    planting_crop,
                    fast_list_click,
                    (input_fast_select, input_fast_list_keycode)
                        .run_if(in_state(PlantingUiState::FastList)),
                    (input_backpack_select, input_backpack_keycode)
                        .run_if(in_state(PlantingUiState::Backpack)),
                )
                    .run_if(in_state(GameState::LevelLoading)),
            )
            .add_observer(next_day_obs)
            .add_observer(can_action)
            .add_observer(harvest_crop)
            .add_observer(hoe_field)
            .add_observer(water_crop)
            .add_observer(del_crop_obs)
            .add_observer(plant_crop)
            .add_observer(place_furniture)
            .add_observer(modify_goods_obs)
            .add_observer(first_load_game_obs)
            .add_observer(save_game_obs);
    }
}
